#SingleInstance ignore

; AHKHID.ahk constants
#Include %A_ScriptDir%\lib\AHKHID.ahk

;Create GUI to receive messages
Gui, +LastFound
hGui := WinExist()

;Intercept WM_INPUT messages
WM_INPUT := 0xFF
OnMessage(WM_INPUT, "InputMsg")

;Register Presenter 8000 with RIDEV_INPUTSINK (so that data is received even in the background)
r := AHKHID_Register(12, 1, hGui, RIDEV_INPUTSINK)

;Match any part of window title
SetTitleMatchMode, 2

;Prefix loop
Loop {
    Sleep 1000
    If WinActive("ahk_class AcrobatSDIWindow")
        sPrefix := "AR"
	Else If WinActive("OpenOffice.org Impress")
		sPrefix := "OOI"
    Else sPrefix := "default"
}

Return

InputMsg(wParam, lParam) {
    Local devh, iKey, sLabel
   
    Critical
   
    ;Get handle of device
    devh := AHKHID_GetInputInfo(lParam, II_DEVHANDLE)
   
    ;Check for error
    If (devh <> -1) ;Check that it is the Presenter 8000
        And (AHKHID_GetDevInfo(devh, DI_DEVTYPE, True) = RIM_TYPEHID)
        And (AHKHID_GetDevInfo(devh, DI_HID_VENDORID, True) = 1118)
        And (AHKHID_GetDevInfo(devh, DI_HID_PRODUCTID, True) = 1811)
        And (AHKHID_GetDevInfo(devh, DI_HID_VERSIONNUMBER, True) = 256) {
       
        ;Get data
        iKey := AHKHID_GetInputData(lParam, uData)
       
        ;Check for error
        If (iKey <> -1) {
           
            ;Get keycode (located at the 4th byte)
            iKey := NumGet(uData, 3, "UChar")
           
            ;Call the appropriate sub if it exists
            sLabel := sPrefix "_" iKey
            If IsLabel(sLabel)
                Gosub, %sLabel%
        }
    }
}

AR_9: ;Left
    SendInput {Left}
Return

AR_8: ;Right
    SendInput {Right}
Return

AR_11: ;Full Screen
    SendInput ^l
Return

OOI_9: ;Left
    SendInput {Left}
Return

OOI_8: ;Right
    SendInput {Right}
Return

OOI_11: ;Blank Screen
    SendInput b
Return